package com.threeti.jiayusms.web.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.threeti.jiayusms.domain.model.CommunicationTime;
import com.threeti.jiayusms.domain.service.communicationtime.CommunicationTimeQueryService;
import com.threeti.jiayusms.domain.service.communicationtime.CommunicationTimeUpdateService;
import com.threeti.jiayusms.domain.util.DateUtil;
import com.threeti.jiayusms.domain.util.TemplateIdGenerator;
import com.threeti.jiayusms.framework.util.DynaSearchCriteria;
import com.threeti.jiayusms.framework.util.PagedListHandler;
import com.threeti.jiayusms.framework.web.PagedQueryControllerAdapter;
import com.threeti.jiayusms.web.form.TimeTemplateForm;

@Controller
public class TimeTemplateMainController {

	 @Autowired
	  private CommunicationTimeQueryService queryService;
	 
	 @Autowired
	  private CommunicationTimeUpdateService updateService;

	  @RequestMapping(value="/timeTemplateMain.html")
	  public String enterTimeTemplateMain(HttpServletRequest request,Map modelMap) {
	    PagedQueryControllerAdapter pagedHelper = new PagedQueryControllerAdapter();
	    int[] index = pagedHelper.getIndex(request);    
	    PagedListHandler<CommunicationTime> pagedTimeTemplates = queryService.findAllTimeTemplatesByPage(index[0], index[1]);
	    pagedHelper.processPage(pagedTimeTemplates, request);
	    
	    modelMap.put("timeTemplates", pagedTimeTemplates.getPageList());
	    
	    return "timetemplate/timeTemplateMain";
	  }
	  
	  @RequestMapping(value="/newTimeTemplate.html", method=RequestMethod.POST)
	  public String newTimeTemplate(@ModelAttribute TimeTemplateForm form) {
	  	CommunicationTime timeTemplate = new CommunicationTime();

	  	timeTemplate.setAvailDate(DateUtil.stringToDate(form.getAvailDate(),"yyyy-MM-dd"));
	  	timeTemplate.setWorkFlag(form.getWorkFlag());
	  	timeTemplate.setTimeDescription(form.getTimeDescription());
	  	timeTemplate.setTimeSegment(form.getTimeSegment());
	  	
	  	CommunicationTime rtnTimeTemplate = updateService.storeCommunicationTime(timeTemplate);
	  	String templateId = TemplateIdGenerator.generateTempalteIdBy("SJ", rtnTimeTemplate);
	  	rtnTimeTemplate.setCustomId(templateId);
	  	updateService.storeCommunicationTime(rtnTimeTemplate);
	    
	    return "redirect:timeTemplateMain.html";
	  }
	  
	  @RequestMapping(value="/removeTimeTemplate.html", method = RequestMethod.POST)
	  public String removeTimeTemplate(@RequestParam("keys") String pks,
	                            Map modelMap, HttpServletRequest request) {
	    String[] timeTemplateIds = pks.split(",");

	    for (int i = 0; i < timeTemplateIds.length; i++) {
	      updateService.removeCommunicationTimeBy(Long.valueOf(timeTemplateIds[i]));
	    }
	    return "redirect:/timeTemplateMain.html";
	  }
	  
	  
	  
	  @RequestMapping(value="/enterTimeTemplateEditForm.html", method = RequestMethod.GET)
	  public String enterEditTimeTemplate(@RequestParam("updatekey") String timeTemplateId,
	                            Map modelMap, HttpServletRequest request) {
	  	CommunicationTime template = queryService.findCommunicationTimeBy(Long.valueOf(timeTemplateId));
	  	
	  	modelMap.put("timeTemplate", template);
	    
	    return "timetemplate/editTimeTemplateForm";
	  }
	  
	  @RequestMapping(value="/editTimeTemplate.html", method = RequestMethod.POST)
	  public String editTimeTemplate(@ModelAttribute TimeTemplateForm form,
	                            Map modelMap, HttpServletRequest request) {
	  	CommunicationTime template = queryService.findCommunicationTimeBy(Long.valueOf(form.getUpdatekey()));
	  	
	  	template.setAvailDate(DateUtil.stringToDate(form.getAvailDate(),"yyyy-MM-dd"));
	  	template.setWorkFlag(form.getWorkFlag());
	  	template.setTimeDescription(form.getTimeDescription());
	  	template.setTimeSegment(form.getTimeSegment());
  	
	  	updateService.storeCommunicationTime(template);
	  	
	  	modelMap.put("timeTemplate", template);
	    
	    return "redirect:/timeTemplateMain.html";
	  }
	
	  @RequestMapping(value="/searchTimeTemplateByPage.html")
	  public String searchTimeTemplate(HttpServletRequest request, @ModelAttribute TimeTemplateForm form, Map modelMap) {

	  	DynaSearchCriteria dynaSearchCriteria = null;
	  	
	    PagedQueryControllerAdapter pagedHelper = new PagedQueryControllerAdapter();
	  	if (pagedHelper.isAnyPageSearch(request)) {
	  		dynaSearchCriteria = (DynaSearchCriteria)modelMap.get(TimeTemplateForm.SESSION_DYNA_CRITERIA_KEY);
	  	} else {
	      modelMap.put(TimeTemplateForm.SESSION_DYNA_CRITERIA_KEY, (dynaSearchCriteria = form.buildDynaSearchCriteriaBy("com.threeti.jiayusms.interfaces.TimeTemplateDynaCriteriaDto")));  		
	  	}
	    int[] index = pagedHelper.getIndex(request);    
	    PagedListHandler<CommunicationTime> pagedTimeTemplates = queryService.findTimeTemplatesByPage(dynaSearchCriteria, index[0], index[1]);
	    pagedHelper.processPage(pagedTimeTemplates, request);
	    
	    modelMap.put("timeTemplates", pagedTimeTemplates.getPageList());
	    
	    return "timetemplate/timeTemplateMain";
	  }
	  
}
